package com.tusuo.stds.ui.ad;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.ui.splash.SplashActivity_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(AdActivity.class)
@EActivity(R.layout.activity_ad)
public class AdActivity extends Activity implements ViewPager.OnPageChangeListener {
    @ViewById(R.id.viewpager_ad)
    ViewPager mViewPager;
    @ViewById(R.id.ad_btn_next)
    Button mOkButton;

    @Inject
    SharedPreferences mSharedPref;

    // 引导图片资源
    private static final int[] pics = {
            R.drawable.intro_img_1,
            R.drawable.intro_img_2,
            R.drawable.intro_img_3,
            R.drawable.intro_img_4,
            R.drawable.intro_img_5,
            R.drawable.intro_img_6
    };
    // 底部小点图片
    private ImageView[] dots;
    // 记录当前选中位置
    private int currentIndex;

    @AfterInject
    void initDI() {
        Injector.initAdActivityComponent().inject(this);

        if (mSharedPref.getBoolean("firstStart", true)) {
            SharedPreferences.Editor editor = mSharedPref.edit();
            editor.putBoolean("firstStart", false);
            editor.apply();
        } else {
            toLogin();
        }
    }

    @AfterViews
    void initViews() {
        List<View> views = new ArrayList<>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        // 初始化引导图片
        for (int resId : pics) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setBackgroundResource(resId);
            views.add(iv);
        }

        // 初始化Adapter
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(views);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_ad);

        mViewPager.setAdapter(viewPagerAdapter);
        // 绑定回调
        mViewPager.addOnPageChangeListener(this);

        // 初始化底部小圆点
        initDots();
    }

    @Click(R.id.ad_btn_next)
    void toLogin() {
        Intent intent = new Intent();
        intent.setClass(this, SplashActivity_.class);
        startActivity(intent);
        finish();
    }

    /**
     * 引导小点被点击时调用
     */
    @Click({R.id.iv_ad_dot_1,
            R.id.iv_ad_dot_2,
            R.id.iv_ad_dot_3,
            R.id.iv_ad_dot_4,
            R.id.iv_ad_dot_5,
            R.id.iv_ad_dot_6})
    void changeCurView(View clickedView) {
        int position = (Integer) clickedView.getTag();
        setCurView(position);
        setCurDot(position);
    }

    /**
     * 当前页面被滑动时调用
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    /**
     * 当新的页面被选中时调用
     */
    @Override
    public void onPageSelected(int position) {
        // 设置底部小点选中状态
        setCurDot(position);

        if(position == pics.length-1){
            mOkButton.setVisibility(View.VISIBLE);
        }else{
            mOkButton.setVisibility(View.GONE);
        }
    }

    /**
     * 当滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int state) {}

    /**
     * 初始化引导小点
     */
    private void initDots() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_ad);

        dots = new ImageView[pics.length];

        // 循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            dots[i] = (ImageView) linearLayout.getChildAt(i);
            // 都设为非选中状态
            dots[i].setEnabled(true);
//            dots[i].setOnClickListener(this);
            // 设置位置tag，方便取出与当前位置对应
            dots[i].setTag(i);
        }

        currentIndex = 0;
        // 设置为选中状态
        dots[currentIndex].setEnabled(false);
    }

    /**
     * 设置当前的引导页
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }

        mViewPager.setCurrentItem(position);
    }

    /**
     * 设置当前引导小点的背景
     */
    private void setCurDot(int position) {
        if (position < 0 || position > pics.length - 1 || currentIndex == position) {
            return;
        }

        dots[position].setEnabled(false);
        dots[currentIndex].setEnabled(true);

        currentIndex = position;
    }
}
